package ar.com.angrymachine.hunter.utils;

import java.util.Vector;

import ar.com.angrymachine.hunter.Activity;
import ar.com.angrymachine.hunter.activities.BossAltarActivity;
import ar.com.angrymachine.hunter.activities.ExploreActivity;
import ar.com.angrymachine.hunter.activities.SlumberActivity;
import ar.com.angrymachine.hunter.gameplay.Database;
import ar.com.angrymachine.hunter.gameplay.Dungeon;
import ar.com.angrymachine.hunter.gameplay.Quest;
import ar.com.angrymachine.hunter.gameplay.Room;
import ar.com.angrymachine.utils.Util;

public class HubDungeonGenerator implements DungeonGenerator
{
	private Dungeon currentDungeon;
	private Vector deadEndNodes = new Vector(); // all the nodes with no further exploration discoveries
	
    public Dungeon generate(Quest quest)
    {
    	// this is a lucifer-type dungeon
    	// there is one kickass creature that
    	// must be killed through the defiling of its altar
    	// the altar is somewhere in the dungeon
    	// only one undiscovered location hosts the altar
    	// the creature is always available
    	
    	// decide the amount of rooms in the dungeon
    	int rooms = Util.getRandom(6,10);
    	// spawn a random generated branching dungeon
    	currentDungeon = new Dungeon(quest);
    	createDungeonNetwork(currentDungeon, rooms);
    	// to add the final altar, choose a dead end exploration node
    	ExploreActivity altarNode = (ExploreActivity) deadEndNodes.elementAt(Util.getRandom(0, deadEndNodes.size()-1));
    	altarNode.addDiscovery(new BossAltarActivity(5000), altarNode.maxExploration);
    	// add the final boss
    	currentDungeon.addActivityInTail(new SlumberActivity(Database.lucifer, 50000));
    	return currentDungeon;
    }
    
    private Dungeon createDungeonNetwork(Dungeon dungeon, int rooms)
    {
    	// create the main hub room
    	Room hubRoom = (Room) Database.getRandomLeveledEntity(0, Database.rooms);
    	ExploreActivity hub = new ExploreActivity(hubRoom, 13000);
    	// spawn all the available rooms
        int[] times = DungeonGenerationUtils.createEventTimeline(rooms, hub.maxExploration);
        for (int i = 0; i < times.length; i++)
        {
        	// create a sensible room type here
        	ExploreActivity newRoom = DungeonGenerationUtils.createRoom(currentDungeon, i+1);
        	Activity finalActivity = DungeonGenerationUtils.lockBehindDoor(currentDungeon, newRoom, 33);
        	hub.addDiscovery(finalActivity, times[i]);
            // after the first half, put the altar
            if(i >= times.length/2)
            {
            	deadEndNodes.add(newRoom);
            }
        }
        // add some random stuff
        DungeonGenerationUtils.populateLocation(currentDungeon, hub, rooms/2, Util.getRandom(hub.maxExploration/10000, (hub.maxExploration/10000)*2));
        dungeon.addActivity(hub);
        return dungeon;    	
    }    

}
